package sm.upgrade;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.upgrade.api.UpgradePhase;
import sm.util.RADFunction;
import sm.util.SMMap;

public class SLATypeUpgrade implements UpgradePhase {

	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	@Override
	public void upgrade() {
		logger.info("Updating the type of the SLA records...");
		List<Object> slas = template.find("from sla");
		if (slas != null) {
			for (Object sla : slas) {
				SMMap slaMap = (SMMap) sla;
				slaMap.put("type",
						RADFunction.nullsub(slaMap.get("type"), "customer"));
				template.update("sla", slaMap);
			}
		}

		logger.info("  Finished updating the type of the SLA records.");
	}

}
